{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图像对比度增强\n",
    "\n",
    "---\n",
    "个人信息:  \n",
    "- 姓名: 罗贺祥\n",
    "- 年级: 2022\n",
    "- 专业: 智能科学与技术\n",
    "- 班级: 2班\n",
    "---\n",
    "\n",
    "- 诚信守则:  \n",
    "    - 我承诺该报告内容为我个人书写非直接抄袭所得。\n",
    "    - 我可以解释答题思路并复现我的答案。\n",
    "    - 如无法实现以上要求，我愿意承担由此带来的后果。\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2 as cv\n",
    "\n",
    "\n",
    "#  读取pupil.tiff灰度图\n",
    "img  =cv.imread(\"pupil.tiff\",0)\n",
    "\n",
    "\n",
    "#  给图形做极大极小值归一  min  max  normalization\n",
    "img_max  =  img.max()\n",
    "  #  获取img最大值\n",
    "img_min  =  img.min()\n",
    "  #  获取img最小值\n",
    "img_minmax_equalize_hist  =  (img-img_min)/(img_max-img_min)\n",
    "  #  得到img进行极小极大值归一的结果\n",
    "\n",
    "img_equalize_hist_1  =cv.equalizeHist(img)\n",
    "  #  用OpenCV直接进行全局直方图均衡\n",
    "\n",
    "hist,  _  =  np.histogram(img.flatten(), 256, [0, 256])\n",
    "  #  获得img的直方图(分256个区间)\n",
    "cdf  =  hist.cumsum()\n",
    "    #  获得hist对应的累积直方图\n",
    "cdf  =  (hist.cumsum()-cdf.min())/(cdf.max()-cdf.min())\n",
    "    #  把累积直方图进行极大极小值归一获得累积频率直方图\n",
    "img_equalize_hist_2  =  cdf[img]\n",
    "    #  通过累积频率直方图对img进行全局直方图均衡\n",
    "\n",
    "#  创建一个CLAHE算法对象,  8*8的小窗,  阈值为6\n",
    "CLAHE  =  cv.createCLAHE(clipLimit=6,tileGridSize=(8,8))\n",
    "  \n",
    "img_equalize_adapthist  =  CLAHE.apply(img)\n",
    "    #  使用CLAHE  进行自适应直方图均衡\n",
    "\n",
    "fg,  ax  =  plt.subplots(5,  2,  figsize=(5,  10))\n",
    "#  用完整的数据范围打印图像及其直方图\n",
    "ax[0,  0].imshow(img,  cmap='gray',  vmin=0,  vmax=255)\n",
    "ax[0,  1].set_title('original  low  contrast  image')\n",
    "ax[0,  1].hist(img.ravel(),  bins=256,  range=[0,  255])\n",
    "\n",
    "#  最小最大值归一\n",
    "ax[1,  0].imshow(img_minmax_equalize_hist,  cmap='gray',  vmin=0,  vmax=1)\n",
    "ax[1,  1].set_title('min-max  normalized  image')\n",
    "ax[1,  1].hist(img_minmax_equalize_hist.ravel(),  bins=256,  range=[0,  1])\n",
    "\n",
    "#  全局直方图均衡\n",
    "ax[2,  0].imshow(img_equalize_hist_1,  cmap='gray')\n",
    "ax[2,  1].set_title('global  equalization:  OpenCV')\n",
    "ax[2,  1].hist(img_equalize_hist_1.ravel(),  bins=256)\n",
    "\n",
    "ax[3,  0].imshow(img_equalize_hist_2,  cmap='gray')\n",
    "ax[3,  1].set_title('global  equalization:  Numpy')\n",
    "ax[3,  1].hist(img_equalize_hist_2.ravel(),  bins=256)\n",
    "\n",
    "#  自适应直方图均衡\n",
    "ax[4,  0].imshow(img_equalize_adapthist,  cmap='gray')\n",
    "ax[4,  1].set_title('CLAHE:  8*8,  2')\n",
    "ax[4,  1].hist(img_equalize_adapthist.ravel(),  bins=256)\n",
    "\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.savefig('out.png',  bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
